home *** CD-ROM | disk | FTP | other *** search
/ 500 MB Nyheder Direkte fra Internet 2 / 500 MB nyheder direkte fra internet CD 2.iso / start / data / text / hints.txt < prev    next >
Text File  |  1994-09-21  |  16KB  |  329 lines

  1.  
  2.                     DEBUGGING
  3.   If you write and run your own program, it probably won't work.
  4.   Your first reaction will be to blame the computer. Don't!
  5.   The probability is 99.99% that the fault is yours. Your program 
  6. contains an error. An error is called a bug. Your next task is to 
  7. debug the program, which means get the bugs out.
  8.   Bugs are common; top-notch programmers make errors all the 
  9. time. If you write a program that works perfectly on the first 
  10. run and doesn't need debugging, it's called a gold-star program, 
  11. and means you should have tried writing a harder one instead!
  12.   It's easy to write a program that's almost completely correct, 
  13. but hard to find the little bug that's fouling it up. Most of the 
  14. time you spend at the computer will be devoted to debugging.
  15.   Debugging can be fun. Hunting for the bug is like going on a 
  16. treasure hunt ___ or solving a murder mystery. Pretend you're 
  17. Sherlock Holmes. Your mission: to find the bug and squish it! 
  18. When you squish it, have fun: yell out, ``Squish!''
  19.   How can you tell when a roomful of programmers is happy? 
  20. Answer: when you hear continual cries of ``Squish!''
  21.   To find a bug, use three techniques:
  22. Inspect the program.
  23. Trace the computer's thinking.
  24. Shorten the program.
  25.   Here are the details. . . . 
  26.  
  27.                Inspect the program
  28.   Take a good, hard look at the program. If you stare hard 
  29. enough, maybe you'll see the bug.
  30.   Ask the computer to help you. Make the computer print the 
  31. entire program. To do that, type:
  32. LIST
  33.   Popular typos Usually, the bug will turn out to be just a 
  34. typing error, a typo. For example. . . . 
  35. Maybe you typed the letter O instead of a zero? Typed zero 
  36. instead of the letter O?
  37. Typed I instead of 1? Typed 1 instead of I?
  38. Pressed the SHIFT key when you weren't supposed to? Forgot to 
  39. press it?
  40. Typed an extra letter? Omitted a letter?
  41. Typed a line you thought you hadn't? Omitted a line?
  42.   Fix your strings You must put quotation marks around each 
  43. string, and a dollar sign after each string variable:
  44. Right:A$="JERK"
  45. Wrong:A$=JERK
  46. Wrong:A="JERK"
  47.   Too much? Here are two reasons why the computer might print too 
  48. much:
  49. 1. You forgot the insert the word END into your program.
  50. 2. You forgot to type NEW; so the computer is reprinting part of 
  51. the previous program.
  52.  
  53.  
  54.     Trace the computer's thinking
  55.   If you've inspected the program thoroughly and still haven't 
  56. found the bug, the next step is to trace the computer's thinking. 
  57. Pretend you are the computer. Do what your program says. Do you 
  58. find yourself printing the same wrong answers the computer 
  59. printed? If so, why?
  60.   To help your analysis, make the computer print everything it's 
  61. thinking while it's running your program. For example, suppose 
  62. your program contains lines 10, 20, 30, and 40, and uses the 
  63. variables B, C, and X$. Insert these lines into your program:
  64. 15 PRINT "I'M AT LINE 15.  THE VALUES ARE";B;C;X$
  65. 25 PRINT "I'M AT LINE 25.  THE VALUES ARE";B;C;X$
  66. 35 PRINT "I'M AT LINE 35.  THE VALUES ARE";B;C;X$
  67. 45 PRINT "I'M AT LINE 45.  THE VALUES ARE";B;C;X$
  68. Then type the word RUN. The computer will run the program again; 
  69. but lines 15, 25, 35, and 45 make the computer print everything 
  70. it's thinking. Check what the computer prints. If the computer 
  71. prints what you expect in lines 15 and 25, but prints strange 
  72. values in line 35 (or doesn't even get to line 35), you know the 
  73. bug occurs before line 35 but after line 25; so the bug must be 
  74. in line 30.
  75.   If your program contains hundreds of lines, you might not have 
  76. the patience to insert lines 15, 25, 35, 45, 55, 65, 75, etc. 
  77. Here's a short cut. . . . 
  78.   Halfway down your program, insert a line that says to print all 
  79. the values. Then run your program. If the line you inserted 
  80. prints the correct values, you know the bug lies underneath that 
  81. line; but if the line prints wrong values (or if the computer 
  82. never reaches that line), you know the bug lies above that line. 
  83. In either case, you know which half of your program contains the 
  84. bug. In that half of the program, insert more lines, until you 
  85. finally zero in on the line that contains the bug.
  86.                                                 Shorten the program
  87.                                          When all else fails, 
  88. shorten the program.
  89.                                          Hunting for a bug in a 
  90. program is like hunting for a needle in a haystack: the job is 
  91. easier if the haystack is smaller. So make your program shorter: 
  92. delete the last half of your program. Then run the shortened 
  93. version. That way, you'll find out whether the first half of your 
  94. program is working the way it's supposed to. When you've 
  95. perfected the first half of your program, tack the second half 
  96. back on.
  97.                                          Does your program 
  98. contain a statement whose meaning you're not completely sure of? 
  99. Check the meaning by reading a book or asking a friend; or write 
  100. a tiny experimental program that contains the statement, and see 
  101. what happens when you type RUN.
  102.                                          Hint: before you shorten 
  103. your program (or write tiny experimental ones), SAVE the original 
  104. version, even though it contains a bug. After you've played with 
  105. the shorter versions, retrieve the original and fix it.
  106.                                          The easiest way to write 
  107. a long, correct program is to write a short program first, debug 
  108. it, then add a few more lines, debug them, add a few more lines, 
  109. debug them, etc. In other words, start with a small program, 
  110. perfect it, and then gradually add perfected extras so you 
  111. gradually build a perfected masterpiece. If you try to compose a 
  112. long program all at once ___ instead of building it from 
  113. perfected pieces ___ you'll have nothing more than a mastermess 
  114. ___ full of bugs.
  115.                                          Moral: to build a large 
  116. masterpiece, start with a small masterpiece. Putting it another 
  117. way: to build a skyscraper, begin by laying a good foundation; 
  118. double-check the foundation before you start adding the walls and 
  119. the roof.
  120.  
  121.            ERROR MESSAGES
  122.   If you type a command that the computer can't obey, the 
  123. computer will gripe by printing an error message.
  124.   You can make 6 kinds of errors: syntax errors, numeric errors, 
  125. printer errors, logic errors, disk errors, and advanced errors. 
  126. Here are the most popular error messages printed by the IBM PC, 
  127. in the order you'll probably encounter them.
  128.  
  129.             Syntax errors
  130.   If you say PRIND instead of PRINT, the computer will say SYNTAX 
  131. ERROR. That means the computer hasn't the faintest idea of what 
  132. you're talking about!
  133.   If the computer says you have a SYNTAX ERROR, it's usually 
  134. because you spelled a word wrong, or forgot a word, or used a 
  135. word the computer doesn't understand. It can also result from 
  136. wrong punctuation: check your commas, semicolons, and colons. It 
  137. can also mean you typed a left parenthesis that doesn't have a 
  138. corresponding right parenthesis, or vice versa.
  139.   It can also mean your DATA statement contains a string but your 
  140. READ statement says to read a number instead. To fix that 
  141. problem, change the READ statement by putting a dollar sign at 
  142. the end of the variable's name.
  143.  
  144.            Numeric errors
  145.   In a statement such as PRINT 5+2, the + is called the 
  146. operation. The 5 and 2 are called the operands.
  147.   If you try to say PRINT 5+2 but forget to type the 2, the 
  148. computer will say MISSING OPERAND.
  149.   The biggest number the computer can handle is about 1E38 (which 
  150. means 1 followed by 38 zeros). If you try to go much higher, the 
  151. computer will say OVERFLOW. For example, if you say 1E39, or you 
  152. try to multiply together lots of big numbers, you'll get an 
  153. OVERFLOW ERROR.
  154.   If you try to divide by zero, the computer will say DIVISION BY 
  155. ZERO.
  156.   If you feed the computer a number that's inappropriate, the 
  157. computer will say ILLEGAL FUNCTION CALL. For example, if you say 
  158. DELETE 40, but your program doesn't contain any line whose number 
  159. is 40, the computer will say ILLEGAL FUNCTION CALL.
  160.  
  161.            Printer errors
  162.   If your printer runs out of paper, the computer will say OUT OF 
  163. PAPER.
  164.   When the computer sends a message to a device (such as your 
  165. printer), the computer waits for the device to respond to the 
  166. message. If the computer has waited a long time without receiving 
  167. any response from the device, the computer will give up waiting; 
  168. it will say DEVICE TIMEOUT.
  169.   When you try using your printer, if the computer says DEVICE 
  170. TIMEOUT it means the computer isn't receiving the proper feedback 
  171. from your printer. That's because your printer isn't properly 
  172. attached to the computer, or hasn't been turned on, or isn't in a 
  173. state of being READY.
  174.  
  175.             Logic errors
  176.   If you say GO TO 40 but there isn't a line 40 in your program, 
  177. the computer will say UNDEFINED LINE NUMBER. The computer says 
  178. UNDEFINED LINE NUMBER whenever
  179. you try to GO TO a missing line.
  180.                                          The computer handles two 
  181. types of information: numbers and strings. If you feed the 
  182. computer the wrong type of information ___ if you feed it a 
  183. number when you should have fed it a string, or you feed it a 
  184. string when you should have fed it a number ___ the computer will 
  185. say TYPE MISMATCH.
  186.                                          When you feed the 
  187. computer a string, you must put the string in quotation marks, 
  188. and put a dollar sign after the string's variable. If you forget 
  189. to type the string's quotation marks or dollar sign, the computer 
  190. won't realize it's a string; the computer will think you're 
  191. trying to type a number instead; and if a number would be 
  192. inappropriate, the computer will say TYPE MISMATCH. So when the 
  193. computer says TYPE MISMATCH, it usually means you forgot a 
  194. quotation mark or a dollar sign.
  195.  
  196.                                                     Disk errors
  197.                                          To use the disks, you 
  198. must invent a name for your program. If the name you invent is 
  199. inappropriate, the computer will say BAD FILE NAME.
  200.                                          If you forget to put a 
  201. disk into the disk drive, or you accidentally leave the disk 
  202. drive's door open, the computer will say DISK NOT READY. If the 
  203. disk's surface is scratched or otherwise damaged, the computer 
  204. will say DISK MEDIA ERROR.
  205.                                          If the disk is 
  206. write-protected by a write-protect tab (which stops you from 
  207. writing onto the disk) and you nevertheless try to write 
  208. information onto the disk, the computer will say DISK WRITE 
  209. PROTECT. If you try to write information onto the disk but the 
  210. disk doesn't have enough free space to hold the information, the 
  211. computer will say DISK FULL.
  212.                                          The disk's directory is 
  213. a place on the disk that remembers the names of all the disk's 
  214. programs and files. If the directory is full, so that there's no 
  215. more room to store names of programs, and you nevertheless try to 
  216. put another program onto the disk, the computer will say TOO MANY 
  217. FILES.
  218.                                          If you tell the computer 
  219. to copy JOE from the disk to the RAM (by saying LOAD ``JOE'' or 
  220. RUN ``JOE''), but the computer can't find JOE on the disk, the 
  221. computer will say FILE NOT FOUND.
  222.  
  223.                                                   Advanced errors
  224.                                          If you say READ, but the 
  225. computer can't find any more DATA to read (because the computer 
  226. has read all the DATA already), the computer will say OUT OF 
  227. DATA.
  228.                                          Whenever you say FOR, 
  229. you're supposed to say NEXT, and vice versa. If you say FOR 
  230. without saying NEXT, the computer will say FOR WITHOUT NEXT. If 
  231. you say NEXT without saying FOR, the computer will say NEXT 
  232. WITHOUT FOR. If your program contains the words FOR and NEXT 
  233. several times, but you accidentally have more FOR's than NEXT's 
  234. or more NEXT's than FOR's, the computer will print one of those 
  235. error messages, because the number of FOR's ought to equal the 
  236. number of NEXT's.
  237.                                          If your computer doesn't 
  238. have enough RAM to hold your information, it will say OUT OF 
  239. MEMORY. Part of the RAM is used for handling strings; if that 
  240. part isn't big enough to hold all your strings, the computer will 
  241. say OUT OF STRING SPACE. If you get one of those error messages, 
  242. shorten your program (by splitting it into several smaller 
  243. programs).
  244.  
  245.       CONVENIENCES
  246.   The following commands make your life easier.
  247.  
  248.        Renumbering
  249.   Suppose you've written a sloppy program, like this:
  250. 37 PRINT "SNARL"
  251. 68 PRINT "SMIRK"
  252. 91 GO TO 37
  253.   If you type ___ 
  254. RENUM
  255. LIST
  256. the computer will renumber your program, so that the lines are 
  257. numbered 10, 20, and 30. The computer will say:
  258. 10 PRINT "SNARL"
  259. 20 PRINT "SMIRK"
  260. 30 GO TO 10
  261. Notice that the computer even changed the ``GO TO 37'', so that 
  262. it became ``GO TO 10''!
  263.   If you type ___ 
  264. RENUM 500
  265. LIST
  266. the computer will renumber your program so that it begins at line 
  267. 500. The computer will say:
  268. 500 PRINT "SNARL"
  269. 510 PRINT "SMIRK"
  270. 520 GO TO 500
  271.   Suppose you wrote a program that contained a line numbered 83, 
  272. and you'd like that line to be called ``line 500'' instead. 
  273. Here's how to start renumbering at line 83, so that line 83 
  274. becomes 500, and the next line becomes 600, and the line after 
  275. that becomes 700, etc.:
  276. RENUM 500,83,100
  277. LIST
  278. That command makes the computer renumber the program so that 500 
  279. is the new number for line 83, and the next lines are numbered 
  280. 600, 700, 800, etc. (going up by 100's).
  281.   The RENUM command works on most computers. To find out about 
  282. your computer, read the ``Versions of BASIC'' appendix.
  283.                                              The PAUSE key
  284.                              Magicians often say, ``The hand is 
  285. quicker than the eye.'' The computer's the ultimate magician: the 
  286. computer can print information on the screen much faster than you 
  287. can read it.
  288.                              When the computer is printing too 
  289. fast for you to read, tap the PAUSE key. The computer will pause, 
  290. to let you read what's on the screen.
  291.                              When you've finished reading what's 
  292. on the screen, and want the computer to stop pausing, tap the 
  293. PAUSE key again. Then the computer will continue printing 
  294. rapidly, where it left off.
  295.                              If your eyes are as slow as mine, 
  296. you'll need to use the PAUSE key often! You'll want the computer 
  297. to pause when you're running a program that contains many PRINT 
  298. statements, or if you're running a program that contains a PRINT 
  299. statement in a loop, or if you're LISTing a long program.
  300.                              The PAUSE key is on the Walter 
  301. keyboard. To find out whether it's on your computer's keyboard, 
  302. check the ``Versions of BASIC'' appendix.
  303.  
  304.                                               Apostrophe
  305.                              Occasionally, jot a note, to remind 
  306. yourself what your program does and what the variables stand for. 
  307. Slip the note into your program by putting an apostrophe before 
  308. it:
  309. 10 'THIS PROGRAM IS ANOTHER DUMB EXAMPLE
  310. 20 'WRITTEN BY RUSSY-POO, A STUPID JERK
  311. 30 'IT WAS WRITTEN ON HALLOWEEN, UNDER A FULL MOON, SHADED BY HIS 
  312. FANGS
  313. 40 C=40' BECAUSE RUSS HAS 40 COMPUTERS
  314. 50 H=23' BECAUSE 23 OF HIS COMPUTERS ARE HAUNTED
  315. 60 PRINT C-H' THAT IS HOW MANY COMPUTERS ARE UNHAUNTED AND SAFE 
  316. FOR KIDS
  317. When you type RUN, the computer ignores everything that's to the 
  318. right of an apostrophe. So the computer ignores lines 10, 20, and 
  319. 30; in lines 40 and 50 the computer ignores the ``because . . . 
  320. ''; in line 60 the computer ignores the comments about being 
  321. unhaunted. Since C is 40, and H is 23, line 60 makes the computer 
  322. print:
  323.  17
  324.                              Everything to the right of an 
  325. apostrophe is called a comment (or remark). While the computer 
  326. runs the program, it ignores the comments. But whenever you say 
  327. LIST, the computer will LIST the entire program, including even 
  328. the comments. Although the comments appear in the LIST, they 
  329. don't affect the RUN.